package com.wf.mock.app.mina.old;


import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.prefixedstring.PrefixedStringCodecFactory;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

public class NewClient {

	public static void main(String[] args) throws IOException,
			InterruptedException {

		final String msg = Files.toString(new File("D:\\testPP.txt"),
				Charsets.UTF_8);

		final NioSocketConnector connector = new NioSocketConnector();
		initConnector(connector);

		concurrentSendMessage(msg, connector);
		
//		seriesSendMsg(msg, connector);
		
	}

	private static void seriesSendMsg(final String msg,
			final NioSocketConnector connector) {
		ConnectFuture connectFuture = connector.connect();

		connectFuture.awaitUninterruptibly();

		IoSession session = connectFuture.getSession();
		WriteFuture writeFuture = null;
		
		for (int i = 0; i < 10; i++) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			JSONObject json = JSONObject.parseObject(msg);
			json.put("content", json.getString("content") + " order:" + i
					+ " :sendTime:" + System.currentTimeMillis());
			String message = "PP" + json.toString();
			writeFuture = session.write(message);
		}
		
		writeFuture.awaitUninterruptibly();
		session.close(true);
	}

	private static void concurrentSendMessage(final String msg,
			final NioSocketConnector connector) {
		List<IoSession> sessionList = Lists.newArrayList();
		for (int i = 0; i < 15 ; i++) {
			ConnectFuture connectFuture = connector.connect();

			connectFuture.awaitUninterruptibly();

			IoSession session = connectFuture.getSession();
			sessionList.add(session);
		}

		int size = sessionList.size();
		ExecutorService service = Executors.newFixedThreadPool(size);
		for (final IoSession ioSession : sessionList) {
			service.submit(new Runnable() {
				public void run() {
					for (int i = 0; i < 20; i++) {
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						JSONObject json = JSONObject.parseObject(msg);
						json.put("content", json.getString("content") + " order:" + i
								+ " :sendTime:" + System.currentTimeMillis());
						final String message = "PP" + json.toString();
						ioSession.write(message);
					}
				}
			});
		}
		service.shutdown();
	}

	private static void initConnector(final NioSocketConnector connector) {
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(buildCodecFactory()));
		connector.getFilterChain().addLast("executor", new ExecutorFilter());
		connector.setHandler(new NewClientHandler());

		connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

		connector.setDefaultRemoteAddress(new InetSocketAddress("127.0.0.1",
				9999));
	}

	private static ProtocolCodecFactory buildCodecFactory() {
		return new PrefixedStringCodecFactory(Charsets.UTF_8);
	}

}
